package com.distributer.product.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.distributer.product.model.DistributerProduct;
import com.distributer.product.request.ProductRefreshRequest;
import com.distributer.product.request.ProductUpdateRequest;
import com.distributer.product.request.Request;
import com.distributer.product.service.AsyncRequestHandlerService;
import com.distributer.product.service.DistributerProductService;

@RestController
@RequestMapping("/product")
public class DistributerProductController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DistributerProductController.class);
	
	@Autowired
	private DistributerProductService distributerProductService;
	@Autowired
	private AsyncRequestHandlerService asyncRequestHandlerService;
	@Value("${server.port}")
	private String port;
	
	@RequestMapping("/update/{jbProductCode}/{price}")
	@ResponseBody
	public Map<String, String> update(@PathVariable(value = "jbProductCode", required = true) String jbProductCode, @PathVariable(value = "price", required = true) String price) {
		// 封装更新请求
		DistributerProduct distributerProduct = new DistributerProduct();
		distributerProduct.setProductPrice(price);
		distributerProduct.setProductCode(jbProductCode);
		Request request = new ProductUpdateRequest(distributerProduct, distributerProductService);
		LOGGER.info("产品编码： "+ jbProductCode+" 提交产品更新请求！");
		asyncRequestHandlerService.requestHandler(request);
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("code", "0000");
		result.put("message", "success");
		return result;
	}

	@RequestMapping("/{jbProductCode}")
	@ResponseBody
	public DistributerProduct product(@PathVariable(value = "jbProductCode", required = true) String jbProductCode) {
		// 封装缓存刷新请求
		DistributerProduct distributerProduct = new DistributerProduct();
		distributerProduct.setProductCode(jbProductCode);
		Request request = new ProductRefreshRequest(distributerProduct, distributerProductService);
		LOGGER.info("产品编码： "+ jbProductCode+" 提交产品缓存刷新请求！");
		asyncRequestHandlerService.requestHandler(request);
		// 开始查询数据
		long waitTime = 0;
		while (true) {
			if (waitTime > 200) {
				break;
			}
			DistributerProduct productFromCache = distributerProductService.findDistributerProductByCache(jbProductCode);
			if (productFromCache != null) {
				LOGGER.info("产品编码： "+ jbProductCode +"，从缓存获取到产品信息： "+ JSONObject.toJSONString(productFromCache));
				return productFromCache;
			}
			try {
				waitTime = waitTime + 20;
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// 直接查询数据库然后刷新缓存
		DistributerProduct productFromDB = distributerProductService.findDistributerProductByJbProductCode(jbProductCode);
		if (productFromDB != null) {
			LOGGER.info("产品编码： "+ jbProductCode +"，从数据库获取到产品信息： "+ JSONObject.toJSONString(productFromDB));
			distributerProductService.cacheRefresh(productFromDB);
			return productFromDB;
		}
		return null;
	}
	
	@RequestMapping("/banlance/port")
	@ResponseBody
	public String port(){
		return this.port;
	}
}
